#ifndef USERMANAGER_H
#define USERMANAGER_H

#include <QObject>
#include <QMutex>
#include <QRecursiveMutex>

class DatabaseManager;

class UserManager : public QObject
{
    Q_OBJECT
public:
    mutable QRecursiveMutex  m_mutex;
    void setDatabaseManager(DatabaseManager* );
    enum class UserError {
        Success, ConnectionError, UserExists,
        InvalidInput, UserNotFound, WrongCredentials,
        WeakPassword, QueryFailed
    };

    static UserManager* instance() {
        static QMutex mutex;
        QMutexLocker lock(&mutex);
        static UserManager* instance = nullptr;
        if (!instance) {
            instance = new UserManager();
        }
        return instance;
    }

    //explicit UserManager(QObject *parent = nullptr);

    std::pair<bool, UserError> validateLogin(const QString &username, const QString &password);
    std::pair<bool, UserError> registerUser(const QString &username, const QString &password,
                                          const QString &email, const QString &phone,
                                          const QString &role = "user", const QString &nickname = "");
    std::pair<bool, UserError> resetPassword(const QString &username,
                                           const QString &input,
                                           const QString &newPassword);
    QString getUserRole(const QString& username) ;
    void setCurrentUser(const QString &username, const QString &role);
    QString getCurrentUserRole() const;
    QString getCurrentUsername() const;
    static bool isPasswordStrong(const QString &password);
    QString executeUserQuery(const QString &query);
    void setCurrentUserRole(const QString &role);
    QString executeQuery(const QString &query);

private:
    explicit UserManager(QObject *parent = nullptr);
    QString hashPassword(const QString &password, const QString &salt) const;


    DatabaseManager *databaseManager;
    QString m_currentUsername;
    QString m_currentRole;
};

#endif // USERMANAGER_H
